# -*- coding: utf-8 -*-

from conf.entity.module.grpc_config import GrpcConfig
from com.maycur import config_loader
from com.maycur.grpc.base.grpc_entity import GrpcFullMapping
from com.maycur.grpc.convertor.ice_proto_convertor import IceAndProtoConverter
from com.maycur.grpc.convertor.java_grpc_converter import GrpcAndJavaConverter
from com.maycur.grpc.convertor.servant_transfer import ServantTransfer
from com.maycur.grpc.lark.lark_document_builder import LarkDocumentBuilder


def convert_ice_2_proto(mapping: GrpcFullMapping, the_grpc_config: GrpcConfig):
    """
    convert from .ice to .proto, including 2 modes: new or update convert
    """
    print('start processing convert_ice_2_proto ...')
    ice_converter = IceAndProtoConverter(the_grpc_config, mapping)
    ice_converter.convert_ice_to_proto()
    return ice_converter.result


def convert_java_2_proto(mapping: GrpcFullMapping, the_grpc_config: GrpcConfig):
    """
    convert from Java domain to .proto
    """
    print('start processing convert_java_2_proto ...')


def generate_java_grpc_converter(mapping: GrpcFullMapping, the_grpc_config: GrpcConfig):
    """
    generate Java domain and gRPC convert methods, both way
    method name convention is as follows(take Java domain Employee as example):
        Java to gRPC: newGrpcEmployee
        gRPC to Java: fromGrpcEmployee
    """
    print('start processing generate_java_grpc_converter ...')
    converter = GrpcAndJavaConverter(the_grpc_config, mapping)
    converter.convert_java_and_grpc()
    return converter


def transfer_servant_ice_2_grpc(mapping: GrpcFullMapping, the_grpc_config: GrpcConfig,
                                the_grpc_java_converter: GrpcAndJavaConverter):
    """
    transfer ice servant method to related gRPC servant
    """
    print('start processing convert_ice_servant_2_grpc ...')
    servant_transfer = ServantTransfer(the_grpc_config, mapping, the_grpc_java_converter)
    servant_transfer.transfer()
    return servant_transfer.result


def make_lark_document(mapping: GrpcFullMapping, the_grpc_config: GrpcConfig):
    """
    collect mappings converted from ice to gRPC, then save to lark document in the form of Markdown table
    """
    print('start processing make_lark_document ...')
    lark_document_builder = LarkDocumentBuilder(the_grpc_config, mapping)
    lark_document_builder.make_markdown_table()
    return lark_document_builder.result


if __name__ == '__main__':
    # init grpc config, make including .ice info, .proto.info, Java converter mapping info, lark info
    grpc_config = config_loader.get_grpc_config()
    ice_config = grpc_config.ice_config
    java_config = grpc_config.java_config
    proto_config = grpc_config.proto_config
    convert_config = grpc_config.converter_config
    lark_config = grpc_config.lark_config
    mapping_config = grpc_config.lark_config

    # init GrpcFullMapping
    grpcFullMapping = GrpcFullMapping()
    grpcFullMapping.filter_mapping = grpc_config.mapping_config

    # execute flag
    executable = True

    # whether to generate from ice
    if grpc_config.gen_proto_from_ice and executable:
        executable = convert_ice_2_proto(grpcFullMapping, grpc_config)

    # whether to generate from Java
    if grpc_config.gen_proto_from_java and executable:
        convert_java_2_proto(grpcFullMapping, grpc_config)

    # whether to convert between Java domain and gRPC
    if grpc_config.convert_java_grpc and executable:
        grpc_java_converter = generate_java_grpc_converter(grpcFullMapping, grpc_config)
        executable = grpc_java_converter.result

    # whether to generate gRPC servant from ice servant
    if grpc_config.transfer_servant and executable:
        executable = transfer_servant_ice_2_grpc(grpcFullMapping, grpc_config, grpc_java_converter)

    # whether to generate lark document
    if grpc_config.document_lark and executable:
        executable = make_lark_document(grpcFullMapping, grpc_config)
